<!DOCTYPE html>
<html lang="en">
<head>
  <!-- Use correct character set. -->
  <meta charset="utf-8">
  <!-- Tell IE to use the latest, best version (or Chrome Frame if pre-IE11). -->
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  <!-- Make the application on mobile take up the full browser screen and disable user scaling. -->
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
  <title>Lyon buildings demo</title>
  <script src="../../cesium/Cesium.js"></script>
  <script src="../Source/WorkQueue.js"></script>
  <script src="../Source/WfsGeometry.js"></script>
  <script src="../Source/TileProvider.js"></script>
  <style>
      @import url(../../cesium/Widgets/widgets.css);
      html, body, #cesiumContainer {
          width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden;
      }
  </style>
</head>
<body>
  <div id="cesiumContainer"></div>
  <script>
var wgs84RadiiSquared = [6378137.0 * 6378137.0, 6378137.0 * 6378137.0, 6356752.3142451793 * 6356752.3142451793];
var DEGREES_PER_RADIAN = 180.0 / Math.PI;
var RADIAN_PER_DEGREEE = 1 / DEGREES_PER_RADIAN;
function cartesianFromDregree(longitude, latitude, height) {

    var lat = latitude*RADIAN_PER_DEGREEE;
    var lon = longitude*RADIAN_PER_DEGREEE;
    var cosLatitude = Math.cos(lat);
    var scratchN = [cosLatitude * Math.cos(lon),
                    cosLatitude * Math.sin(lon),
                    Math.sin(lat)];
    var magN = Math.sqrt( scratchN[0]*scratchN[0] +
                          scratchN[1]*scratchN[1] +
                          scratchN[2]*scratchN[2]);
    scratchN[0] /= magN;
    scratchN[1] /= magN;
    scratchN[2] /= magN;

    var scratchK = [wgs84RadiiSquared[0]*scratchN[0],
                    wgs84RadiiSquared[1]*scratchN[1],
                    wgs84RadiiSquared[2]*scratchN[2]];

    var gamma = Math.sqrt(scratchN[0]*scratchK[0] +
                          scratchN[1]*scratchK[1] +
                          scratchN[2]*scratchK[2]);

    scratchK[0] /= gamma;
    scratchK[1] /= gamma;
    scratchK[2] /= gamma;
    scratchN[0] *= height;
    scratchN[1] *= height;
    scratchN[2] *= height;
    return [scratchK[0]+scratchN[0],
            scratchK[1]+scratchN[1],
            scratchK[2]+scratchN[2]];
}

console.log( cartesianFromDregree(4.84, 45.74, 500) );
console.log( Cesium.Cartesian3.fromDegrees(4.84, 45.74, 500) );

if(1){
    var viewer = new Cesium.Viewer('cesiumContainer');
    var extent = new Cesium.Rectangle.fromDegrees(4.84, 45.74, 4.86, 45.76);
    viewer.camera.viewRectangle(extent);
    var scene = viewer.scene;

    var texture_dir = "../../w/textures/"
    var url = "http://localhost/cgi-bin/tinyows?SERVICE=WFS&VERSION=1.0.0&REQUEST=GetFeature&outputFormat=JSON&typeName=tows:textured_citygml_lat_long&FeatureId=tows:textured_citygml_lat_long.1";
    //var url = 'http://localhost/cgi-bin/tinyows');

    /*
    var wq = new WorkQueue('../Source/createWfsGeometry.js',4);
    wq.addTask('t1: hello from main', 
            function(m){
                if (m.data==='done') return false;
                else {
                console.log('t1: thread sent ', m.data);
                return true;
                }
            });
    wq.addTask('t2: hello from main', 
            function(m){
                if (m.data==='done') return false;
                else {
                console.log('t2: thread sent ', m.data);
                return true;
                }
            });
    console.log("done!")
    */

    /*
    var worker = new Worker('../Source/createWfsGeometry.js');
    worker.onmessage = function(e){
        console.log('from worker: ', e.data);
    }
    worker.postMessage(url)
    */

    /*
    var worker = new Worker('../Source/createWfsGeometry.js');
    worker.onmessage = function(e){
        if (e.data === 'done'){
            console.log('worker is done');
        }
        else{
    var geom = new WfsGeometry({
          position: [[[[extent.west*DEGREES_PER_RADIAN,extent.south*DEGREES_PER_RADIAN,500],
                       [extent.east*DEGREES_PER_RADIAN,extent.south*DEGREES_PER_RADIAN,500],
                       [extent.east*DEGREES_PER_RADIAN,extent.north*DEGREES_PER_RADIAN,500],
                       [extent.west*DEGREES_PER_RADIAN,extent.south*DEGREES_PER_RADIAN,500]]],
                       [[[extent.west*DEGREES_PER_RADIAN,extent.south*DEGREES_PER_RADIAN,500],
                       [extent.east*DEGREES_PER_RADIAN,extent.north*DEGREES_PER_RADIAN,500],
                       [extent.west*DEGREES_PER_RADIAN,extent.north*DEGREES_PER_RADIAN,500],
                       [extent.west*DEGREES_PER_RADIAN,extent.south*DEGREES_PER_RADIAN,500]]]],
          st : [[0,0],[1,0],[1,1],[0,0],[0,0],[1,1],[0,1],[0,0]]
          });
            console.log('data', e.data);
            console.log('wfs geometry', WfsGeometry.createGeometry(geom));
            console.log('worker geometry', geometryFromArrays(e.data));
            var primCol = new Cesium.PrimitiveCollection();
            primCol.add(new Cesium.Primitive({
                  geometryInstances : new Cesium.GeometryInstance({ 
                      geometry : geometryFromArrays(e.data)//WfsGeometry.createGeometry(geom)//geometryFromArrays(e.data),
                  }),
                  appearance : new Cesium.MaterialAppearance({
                      color : Cesium.Color.fromBytes(255, 0, 0, 255)
                      //faceForward : true
                    }),
                  asynchronous : false
              }));
            viewer.scene.primitives.add( primCol );
            //viewer.scene.primitives.add(
            //  new Cesium.Primitive({
            //    geometryInstances: new Cesium.GeometryInstance({
            //        geometry: new Cesium.RectangleOutlineGeometry({
            //            rectangle: extent
            //        }),
            //        attributes: {
            //            color: new Cesium.ColorGeometryInstanceAttribute.fromColor(new Cesium.Color.fromBytes(255, 0, 0, 255))
            //        }
            //    }),
            //    appearance: new Cesium.PerInstanceColorAppearance({
            //        flat: true
            //    })
            //}));
        }
    };
    worker.postMessage(url)
    */

    /*
    var primCol = new Cesium.PrimitiveCollection();
    var geom = new WfsGeometry({
          position: [[[[extent[0],extent[1],500],[extent[2],extent[1],500],[extent[2],extent[3],500],[extent[0],extent[1],500]]],[[[extent[0],extent[1],500],[extent[2],extent[3],500],[extent[0],extent[3],500],[extent[0],extent[1],500]]]],
          st : [[0,0],[1,0],[1,1],[0,0],[0,0],[1,1],[0,1],[0,0]]
          });
    primCol.add(new Cesium.Primitive({
          geometryInstances : new Cesium.GeometryInstance({ 
              geometry : geom,
          }) ,
          appearance : new Cesium.MaterialAppearance({
              color : Cesium.Color.fromBytes(255, 0, 0, 255),
              faceForward : true
            }),
          asynchronous : false
      }));
    viewer.scene.primitives.add( primCol );
    */

    //viewer.scene.primitives.add(
    //  new Cesium.Primitive({
    //    geometryInstances: new Cesium.GeometryInstance({
    //        geometry: new Cesium.RectangleOutlineGeometry({
    //            rectangle: extent
    //        }),
    //        attributes: {
    //            color: new Cesium.ColorGeometryInstanceAttribute.fromColor(new Cesium.Color.fromBytes(255, 0, 0, 255))
    //        }
    //    }),
    //    appearance: new Cesium.PerInstanceColorAppearance({
    //        flat: true
    //    })
    //}));

    // terrain
    //viewer.scene.globe.depthTestAgainstTerrain = true;
    //var cesiumTerrainProviderMeshes = new Cesium.CesiumTerrainProvider({
    //    url : '//cesiumjs.org/stk-terrain/tilesets/world/tiles'
    //});
    //viewer.terrainProvider = cesiumTerrainProviderMeshes;

    // Tiler
    viewer.scene.primitives.add(new Cesium.QuadtreePrimitive({
        tileProvider : new WfsTileProvider(
                           'http://localhost/cgi-bin/tinyows', 
                           'tows:textured_citygml_lat_long', 
                           'http://localhost/w/textures',
                           10, 1000)
    }));

    // antialiasing please
    viewer.scene.fxaa = true;
}
  </script>
</body>
</html>
